﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;

namespace QuanLyThuNgan
{
    public partial class Frm_DM_NoiGuiDichVu : DevExpress.XtraEditors.XtraForm
    {
        public Frm_DM_NoiGuiDichVu()
        {
            InitializeComponent();
        }

        string sTenSearch=""; 
        string sColSearch;
        DMNoiGuiDichVuCTL noiGuiDVCtl = new DMNoiGuiDichVuCTL();
        DMNoiGuiDichVuDTO noiGuiDVDto = new DMNoiGuiDichVuDTO();

        private void Frm_DM_NoiGuiDichVu_Load(object sender, EventArgs e)
        {
            DataTable dtloadNoiGuiDV = noiGuiDVCtl.m_SelectAllDMNoiGuiDV();
            gridControlNoiGuiDV.DataSource = dtloadNoiGuiDV;

            gvNoiGuiDV.OptionsBehavior.ReadOnly = true;
            btnInsert.Enabled = true;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
            btnSave.Enabled = false;
            gvNoiGuiDV.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)gridControlNoiGuiDV.DataSource;
            if (dt != null)
            {
                for (int i = 0; i < gvNoiGuiDV.RowCount; i++)
                {
                    DataRow dr = gvNoiGuiDV.GetDataRow(i);
                    gvNoiGuiDV.MoveNext();
                    gvNoiGuiDV.MovePrev();
                    if (dr != null)
                    {
                        noiGuiDVDto.STenNoiGuiDichVu = dr["TenNoiGuiDichVu"].ToString().Trim();
                        noiGuiDVDto.STenVni = Uit.it_Font.UnicodeToVNI(dr["TenNoiGuiDichVu"].ToString().Trim());

                        noiGuiDVDto.BBNgungSuDung = Uit.it_Parse.ToBoolean(dr["bNgungSuDung"].ToString());
                        if (dr["IDNoiGuiDichVu"].ToString() == "")
                        {
                            noiGuiDVCtl.m_InsertDMNoiGuiDV(noiGuiDVDto);
                        }
                        else
                        {
                            noiGuiDVDto.IIDNoiGuiDichVu = Uit.it_Parse.ToInteger(dr["IDNoiGuiDichVu"].ToString());
                            noiGuiDVCtl.m_UpdateDMNoiGuiDV(noiGuiDVDto);
                        }
                    }
                }
                
            }
            XtraMessageBox.Show("Lưu thành công");
            DataTable dtloadNoiGui = noiGuiDVCtl.m_SelectAllDMNoiGuiDV();
            gridControlNoiGuiDV.DataSource = dtloadNoiGui;

            btnSave.Enabled = false;
            btnUpdate.Enabled = true;
            btnInsert.Enabled = true;
            gvNoiGuiDV.OptionsBehavior.ReadOnly = true;
            gvNoiGuiDV.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            gvNoiGuiDV.OptionsBehavior.ReadOnly = false;
            btnSave.Enabled = false;
            btnInsert.Enabled = false;
            gvNoiGuiDV.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;
            //reset load du lieu----------------------
            gvNoiGuiDV.ActiveFilterEnabled = false;
            gvNoiGuiDV.ClearColumnsFilter();
            gvNoiGuiDV.OptionsFilter.Reset();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            gvNoiGuiDV.OptionsBehavior.ReadOnly = false;
            btnSave.Enabled = true;
            btnInsert.Enabled = true;
            btnUpdate.Enabled = false;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnInsert.Enabled = true;
            btnSave.Enabled = false;
            gvNoiGuiDV.OptionsBehavior.ReadOnly = true;
            DataTable dtloadNoiGui = noiGuiDVCtl.m_SelectAllDMNoiGuiDV();
            gridControlNoiGuiDV.DataSource = dtloadNoiGui;
            gvNoiGuiDV.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
            //reset load du lieu----------------------
            gvNoiGuiDV.ActiveFilterEnabled = false;
            gvNoiGuiDV.ClearColumnsFilter();
            gvNoiGuiDV.OptionsFilter.Reset();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DataRow dr = gvNoiGuiDV.GetDataRow(gvNoiGuiDV.FocusedRowHandle);
            if (dr != null)
            {
                noiGuiDVDto.IIDNoiGuiDichVu = Uit.it_Parse.ToInteger(dr["IDNoiGuiDichVu"].ToString());
                noiGuiDVCtl.m_DeleteDMNoiGuiDV(noiGuiDVDto);
                                
            }
            XtraMessageBox.Show("Xóa thành công");
            DataTable dtloadNoiGui = noiGuiDVCtl.m_SelectAllDMNoiGuiDV();
            gridControlNoiGuiDV.DataSource = dtloadNoiGui;
            //reset load du lieu----------------------
            gvNoiGuiDV.ActiveFilterEnabled = false;
            gvNoiGuiDV.ClearColumnsFilter();
            gvNoiGuiDV.OptionsFilter.Reset();
        }

        private void gvNoiGuiDV_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            GridView view = gvNoiGuiDV;
            GridColumn colNoiGui = view.Columns["TenNoiGuiDichVu"];
            int iRowIndex = e.RowHandle;
            //if (sColRow != "bNgungSuDung")
            //{
            string sTenNoiGui = view.GetRowCellValue(e.RowHandle, colNoiGui).ToString().ToUpper().Trim();
            if (sTenNoiGui == "")
            {
                e.Valid = false;
                view.SetColumnError(colNoiGui, "Tên nơi gửi dịch vụ không được trống");
            }

            for (int i = 0; i < gvNoiGuiDV.RowCount; i++)
            {
                if (i != iRowIndex)
                {
                    DataRow drng = gvNoiGuiDV.GetDataRow(i);
                    string sTestNoiGuiDV = drng["TenNoiGuiDichVu"].ToString().ToUpper().Trim();
                    if (sTenNoiGui == sTestNoiGuiDV)
                    {
                        e.Valid = false;
                        XtraMessageBox.Show("Tên nơi gửi dịch vụ không được trùng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        view.SetColumnError(colNoiGui, "Tên nơi gửi dịch vụ không được trùng");
                        return;
                    }
                }
            }        
                    
            //}
            btnSave.Enabled = true;
        }

        string sColRow;
        private void gvNoiGuiDV_FocusedColumnChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedColumnChangedEventArgs e)
        {
            sColRow = e.FocusedColumn.FieldName;
        }
        //set ten can tim----------------------------------------------------------------
        private void gvNoiGuiDV_ColumnFilterChanged(object sender, EventArgs e)
        {
            ColumnView view = gvNoiGuiDV;
            view.ActiveFilter.Add(view.Columns[sColSearch], new ColumnFilterInfo("[" + sColSearch + "] Like '%" + sTenSearch + "%'", ""));
        }
        //tim ten can search---------------------------------------------
        private void gvNoiGuiDV_CellValueChanging(object sender, CellValueChangedEventArgs e)
        {
            sTenSearch = e.Value.ToString();
            sColSearch = e.Column.FieldName;
        }


    }
}